<?php
/**
 * 《生产计划》实现类
 *
 * @author 孙晓晔
 * @version $Id: Plan.class.php 1056 2010-04-08 05:15:48Z sunxy $
 */
require_once(dirname(__FILE__) . '/MoldUtil.class.php');

class Plan extends MoldUtil {
	public $tbl = Constant::tbl_mold_plan;
	
	public $urgent = array(
		'-' => '请选择',
		'1' => '普通',
		'2' => '重要',
		'3' => '紧急',
		
		'9' => '其它',
	);
	
	public $evaluation = array(
		'-' => '请选择',
		'2' => '差',
		'3' => '中',
		'4' => '良',
		'5' => '优',
		
		'0' => '其它',
	);

	public $status = array(
		'-' => '请选择',
		'1' => '等待审核',
		'2' => '等待处理',
		'3' => '正在处理',		
		'4' => '任务完成',
		'5' => '已经评价',
		
		'8' => '取消任务',	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',

		'mold' => '产品',
	
		'emp' => '计划人',
		'worker' => '负责人',
	
		't1' => '开始日期',
		't2' => '完成日期',
	
		'evaluation' => '评价',
		'status' => '状态',
	
		'1' => '创建时间',
		'2' => '更新时间',
	);

	public $groupby = array(
		'-' => '请选择',
		
		'mold' => '产品',
	
		'emp' => '计划人',
		'worker' => '负责人',
	
		'status' => '状态',
		'1' => '日期',
		'2' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
		
		$controller->assign_by_ref('urgent_option', $this->urgent);
		$controller->assign_by_ref('evaluation_option', $this->evaluation);
	}
	
	function tab($controller, $request) {
		$data = array(
			'我的任务' => array('cc'=>'mold.php?do=PlanMy.find', 'js'=>'1'),
			'任务管理' => array('cc'=>'mold.php?do=Plan.find', 'js'=>'1'),
			'业务需求' => array('cc'=>'emp.php?do=Business.find', 'js'=>'1'),
		);
	
		$ret = $this->_tab($data);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'tab');
		return true;
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->assign('t1', strtotime('-1 month'));
		
		$controller->assign('orderby_s', "t2");
		$controller->assign('desc_s', "1");
		
		$controller->assign('status_id', array(2, 3));

		$ab = array(
			'-' => '请选择',
			'1' => '格式1',		
			'2' => '格式2',
			'3' => '格式3',
		);
		$controller->assign_by_ref('ab_option', $ab);
		
		$emp = array(
			// '-' => '请选择',
			'worker' => '负责人',
			'emp' => '计划人',
			
			'oper' => '录入员',			
		);
		
		$controller->assign_by_ref('emp_option', $emp);

		$controller->display($request, 'mold/plan/find');
		return true;
	}

	function _where($request) {
		$worker_s = $this->_input($request['worker_s']);
		
		$name_s = trim(addslashes($request['name_s']));
		
		$date1_s = $request['date1_s'];
		$date2_s = $request['date2_s'];
		
		$urgent_s = $request['urgent_s'];
		$evaluation_s = $request['evaluation_s'];
		
		$emp_t_s = $request['emp_t_s'];
		if ($emp_t_s == 'emp') {
			$request['emp_s'] = $request['emp_a_s'];
		} else if ($emp_t_s == 'oper') {
			$request['oper_s'] = $request['emp_a_s'];
		} else if ($emp_t_s == 'worker') {
			$request['worker_s'] = $request['emp_a_s'];
		}
		
		$worker_s = $this->_input($request['worker_s']);

		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}
		
		if (!empty($urgent_s) && $urgent_s <> '-') {
			$where .= " AND urgent = '" . $urgent_s . "'";
		}

		if (!empty($name_s)) {
			$where .= " AND (name LIKE '%" . $name_s . "%' OR content LIKE '%" . $name_s . "%')";
		}

		if (!empty($date1_s)) {
			$where .= " AND t1 >= '" . $date1_s . "'";
		}

		if (!empty($date2_s)) {
			$where .= " AND t2 >= '" . $date2_s . "'";
		}

		if (!empty($worker_s)) {
			$where .= " AND worker = '" . $worker_s . "'";
		}
		
		if (!empty($evaluation_s) && $evaluation_s <> '-') {
			$where .= " AND evaluation = '" . $evaluation_s . "'";
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}
		
		if ($this->_audit('164') && $this->_audit('165')) {
			return " visible = 1";
		}

		$emp = $_SESSION['id'];
		
		if ($this->_audit('164')) {
			return sprintf(" visible = 1 AND (office = '1' OR worker = '%s')", $emp);
		}
		
		if ($this->_audit('165')) {
			return sprintf(" visible = 1 AND (office = '2' OR worker = '%s')", $emp);
		}

		$emp = $_SESSION['id'];
		
		return sprintf(" visible = 1 AND (emp = '%s' OR worker = '%s' OR oper = '%s')", $emp, $emp, $emp);
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		} else {
			$controller->clear_assign('result');
		}

		$controller->display($request, 'mold/plan/list');
		return true;
	}

	function doc($controller, $request) {
		$this->pretreat($controller);
		
		$this->findPara($controller, $request);

		$request['tpl'] = "doc";

		$insql = $this->_batch_insql($request);
		if (empty($insql)) {
			$controller->assign('baseLink', $this->doGet($request, 'findPost'));

			$this->findPost($controller, $request);
			return false;
		}
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);

		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);

			$row = $this->_orderby_x($controller, $row, $this->orderby_s);
			
			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);

		$controller->display($request, 'mold/plan/list');
		return true;
	}
	
	function xls($controller, $request) {
		$this->pretreat($controller);
		
		$this->findPara($controller, $request);

		$request['tpl'] = "xls";

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {			
			$row = $this->_view(null, $row);

			$row = $this->_orderby_x($controller, $row, $this->orderby_s);
			
			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);
		
		$filename = sprintf("%s.xls", date("YmdHis"));
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);

		$controller->display($request, 'mold/plan/list');
		return true;
	}
	
	function _batch_insql($request) {		
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		
		return $insql;
	}
	
	function _status_color($row) {
		$status = $row['status'];

		if ($status > 2) {
			return '';
		}

		$b1 = time() > strtotime($row['t1']) ? true : false;
		$b2 = time() > strtotime($row['t2']) ? true : false;

		if ($status == 1) {
			if ($b1 && $b2) {
				return 'red';
			}

			if ($b1) {
				return 'yellow';
			}

			return '';
		}

		if ($status == 2) {
			if ($b2) {
				return 'yellow';
			}
		}

		return '';
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->assign('t1', strtotime('1 day'));
		$controller->assign('t2', strtotime('2 day'));

		// $this->time($controller);

		$controller->display($request, 'mold/plan/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
				
		$date = $request['date'];		
		$worker = $this->_input($request['worker']);		
		
		$mold = $this->_input($request['mold']);		
		$urgent = $request['urgent'];

		$name = addslashes($request['name']);
		$content = addslashes($request['content']);
		
		$t1 = $request['t1'];
		$t2 = $request['t2'];
		$day = $request['day'];
		
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, emp, date, worker, mold, urgent, name, content, t1, t2, day, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s',  '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $date, $worker, $mold, $urgent, $name, $content, $t1, $t2, $day, $_SESSION['id']);
		// debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
		
		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$this->time($controller);

		$controller->display($request, 'mold/plan/copy');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$this->time($controller);

		$controller->display($request, 'mold/plan/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$emp = $this->_input($request['emp']);
				
		$date = $request['date'];		
		$worker = $this->_input($request['worker']);
		
		$mold = $this->_input($request['mold']);
		$urgent = $request['urgent'];

		$name = addslashes($request['name']);
		$content = addslashes($request['content']);
		
		$t1 = $request['t1'];
		$t2 = $request['t2'];
		$day = $request['day'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET emp = '%s', date = '%s', worker = '%s', mold = '%s', urgent = '%s', ";
		$fmt .= "name = '%s', content = '%s', t1 = '%s', t2 = '%s', day = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $emp, $date, $worker, $mold, $urgent, $name, $content, $t1, $t2, $day, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);
		
		$this->time($controller);

		$controller->display($request, 'mold/plan/view');
		return true;
	}

	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}

		$row['mold_t'] = Name::mold($row['mold']);
		$row['worker_t'] = Name::emp($row['worker']);
		$row['urgent_t'] = $this->urgent[$row['urgent']];
		
		$row['evaluation_t'] = $this->evaluation[$row['evaluation']];

		$row['t3'] = $this->_t3($row['t2']);

		$row = $this->_view_t($row);

		return $row;
	}
	
	function _t3($t2) {		
		if (strtotime($t2) < time()) {
			return 0;
		}
		
		return $this->date_diff('d', date("Y-m-d"), $t2);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_id($controller, $request) {
		$q = $request['q'];
		$data = $this->_ajax_id($q);
		echo json_encode($data);
	}

	function _ajax_id($q) {
		if (!preg_match("/^\d+$/i", $q)) {
			return array();
		}

		$key = "ajax.mold.plan_id_" . $q;
		// debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$this->piece = $this->code_option("2");
		$this->technics = $this->code_option("3");

		$db = newdb($this->dsn_r);
		$fmt = "select id, piece, technics, emp from %s where id = %s";
		$sql = sprintf($fmt, $this->tbl, $q);
		debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row['piece_t'] = $this->piece[$row['piece']];
			$row['technics_t'] = $this->technics[$row['technics']];

			$row['emp_t'] = Name::emp($row['emp']);

			$ret[] = $row;
		}

		if (is_object($mc) && count($ret) > 0) {
			$mc->set($key, $ret, 0, _strtotime("1 day"));
		}

		return $ret;
	}
	
	function verify($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);
		
		$email = array(
			'-' => '请选择',
			'1' => '是',		
			'2' => '否',
		);
		$controller->assign_by_ref('email_option', $email);

		$controller->display($request, 'mold/plan/verify');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function verifyPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$email = $request['email'];
		
		$content = addslashes($request['content']);
		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET content = concat(content, '%s'), status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $content, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}
		
		clean($this->tbl . "_id_" . $id);
		$row = $this->_view($id);
		if ($email == '1') {			
			$from = $_SESSION['id'];
	
			$to = $row['worker'];
			// $to = "101";
			$row['to'] = Name::emp($to);
	
			$controller->assign_by_ref('result', $row);
			
			$subject = sprintf('任务信息 %s %s', $row['id'], $row['status_t']);
			$body = $controller->sunxy($request, 'mold/plan/body_2');
			// debug($body);
			
			$this->_notice($from, $to, $subject, $body, 1);
		}

		// $this->_notice_9($id, $status);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function beginPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$email = $request['email'];
		
		$content = addslashes($request['content']);
		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET content = concat(content, '%s'), status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $content, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}
		
		clean($this->tbl . "_id_" . $id);
		$row = $this->_view($id);
		if ($email == '1') {			
			$from = $_SESSION['id'];
	
			$to = $row['emp'];
			// $to = "101";
			$row['to'] = Name::emp($to);
	
			$controller->assign_by_ref('result', $row);
			
			$subject = sprintf('任务信息 %s %s', $row['id'], $row['status_t']);
			$body = $controller->sunxy($request, 'mold/plan/body_3');
			// debug($body);
			
			$this->_notice($from, $to, $subject, $body, 1);
			if ($row['emp'] <> $row['oper']) {
				$this->_notice($from, $row['oper'], $subject, $body, 1);
			}
		}

		// $this->_notice_9($id, $status);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function finishPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$email = $request['email'];
		
		$content = addslashes($request['content']);
		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET content = concat(content, '%s'), status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $content, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		clean($this->tbl . "_id_" . $id);
		$row = $this->_view($id);
		if ($email == '1') {			
			$from = $_SESSION['id'];
	
			$to = $row['emp'];
			// $to = "101";
			$row['to'] = Name::emp($to);
	
			$controller->assign_by_ref('result', $row);
			
			$subject = sprintf('任务信息 %s %s', $row['id'], $row['status_t']);
			$body = $controller->sunxy($request, 'mold/plan/body_4');
			// debug($body);
			
			$this->_notice($from, $to, $subject, $body, 1);
			if ($row['emp'] <> $row['oper']) {
				$this->_notice($from, $row['oper'], $subject, $body, 1);
			}
		}
		
		// $this->_notice_9($id, $status);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function evaluation($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);
		
		$email = array(
			'-' => '请选择',
			'1' => '是',		
			'2' => '否',
		);
		$controller->assign_by_ref('email_option', $email);

		$controller->display($request, 'mold/plan/evaluation');
		return true;
	}
	
	function evaluationPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$evaluation = $request['evaluation'];
		$email = $request['email'];
		
		$content = addslashes($request['content']);
		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET content = concat(content, '%s'), evaluation = '%s', status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $content, $evaluation, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		clean($this->tbl . "_id_" . $id);
		$row = $this->_view($id);
		if ($email == '1') {			
			$from = $_SESSION['id'];
	
			$to = $row['worker'];
			// $to = "101";
			$row['to'] = Name::emp($to);
	
			$controller->assign_by_ref('result', $row);
			
			$subject = sprintf('任务信息 %s %s', $row['id'], $row['status_t']);
			$body = $controller->sunxy($request, 'mold/plan/body_evaluation');
			// debug($body);
			
			$this->_notice($from, $to, $subject, $body, 1);
		}
		
		// $this->_notice_9($id, $status);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function reason($controller, $request) {
		$this->pretreat($controller);

		$status = $request['status'];
		$controller->assign_by_ref('reason', $this->status[$status]);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$this->time($controller);

		$controller->display($request, 'mold/plan/reason');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function reasonPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$content = addslashes($request['content']);
		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET content = concat(content, '%s'), status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $content, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function report($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		$admin_s = $request['admin_s'];
		
		require_once 'Mold.class.php';
		$mold = new Mold();
		$controller->assign_by_ref('mold', $mold->_view($id));

		$db = newdb($this->dsn_r);
		$sql = "SELECT * FROM " . $this->tbl;

		$where = " where mold = '" . $id . "'";

		if (!empty($admin_s) && $admin_s <> '-') {
			$where .= " AND admin = '" . $admin_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		$orderby = 'order by t1';
		$sql .= $where . $orderby;
		// debug($sql);
		$result = $db->query($sql);

		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row['admin_t'] = Name::emp($row['admin']);

			$row['mold_t'] = Name::mold($row['mold']);

			$row['piece'] = $this->piece[$row['piece']];
			$row['technics'] = $this->technics[$row['technics']];

			$row['emp_t'] = Name::emp($row['emp']);

			$time = $row['tt'];

			$row['hour'] = floor($time / 60);
			$row['minute'] = $time % 60;

			$row['status_t'] = $this->status[$row['status']];

			$row = $this->aa($row);

			$data[] = $row;
		}

		$controller->assign_by_ref('result', $data);
		$controller->display($request, 'mold/plan/report');
		return true;
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function pp($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		require_once('Mold.class.php');
		if (class_exists("Mold")) {
			$mold = new Mold();
			$mold->bb($controller, $request);
		}

		$db = newdb($this->dsn_r);
		$sql = "SELECT * FROM " . $this->tbl;

		$where = " where mold = '" . $id . "'";

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		$orderby = 'order by t1';
		$sql .= $where . $orderby;
		// debug($sql);
		$result = $db->query($sql);

		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row['admin_t'] = Name::emp($row['admin']);

			$row['mold_t'] = Name::mold($row['mold']);

			$row['piece'] = $this->piece[$row['piece']];
			$row['technics'] = $this->technics[$row['technics']];

			$row['emp_t'] = Name::emp($row['emp']);

			$time = $row['tt'];

			$row['hour'] = floor($time / 60);
			$row['minute'] = $time % 60;

			$row['t1_t'] = strtotime($row['t1']);
			$row['t2_t'] = strtotime($row['t2']);

			$row['status_t'] = $this->status[$row['status']];

			$row = $this->aa($row);

			$data[] = $row;
		}

		$controller->assign_by_ref('result', $data);

		$a = $this->pp1($id);

		$t1 = strtotime($a['t1']);
		$t2 = strtotime($a['t2']);

		$len = $this->d($t1, $t2);

		$dd = array();
		for ($i = 0; $i <= $len; $i++) {
			$d['t'] = strtotime("$i day", $t1);
			$d['a'] = date('m-d', $d['t']);

			$dd[] = $d;
		}

		$controller->assign_by_ref('dd', $dd);

		$controller->display($request, 'mold/plan/pp');
		return true;
	}

	function pp1($mold) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE mold = '%s' ORDER BY %s LIMIT 1";
		$sql = sprintf($fmt, $this->tbl, $mold, 't1');
		// echo $sql;

		$result = $db->query($sql);
		$row = $db->fetch_array($result);
		$ret['t1'] = $row['t1'];

		$sql = sprintf($fmt, $this->tbl, $mold, 't1 desc');

		$result = $db->query($sql);
		$row = $db->fetch_array($result);
		$ret['t2'] = $row['t1'];

		return $ret;
	}

	function aa($data) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE plan = '%s'";
		$sql = sprintf($fmt, Constant::tbl_mold_produce, $data['id']);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			$data['_t1'] = $row['t1'];
			$data['_t2'] = $row['t2'];
			$data['_tt'] = $row['tt'];

			if (strtotime($data['t2']) < strtotime($row['t2'])) {
				$data['_t2_status'] = 'red';
			}

			if ($data['tt'] < $row['tt']) {
				$data['_tt_status'] = 'red';
			}

			$data['work'] = $this->work[$row['work']];
		} else {
			if ($data['status'] > 1) {
				$data['_t1'] = date("Y-m-d", strtotime($data['modifytime']));
			}

			if (strtotime($data['t2']) < time()) {
				$data['_t2_status'] = 'red';
			}
		}

		if (strtotime($data['t1']) < strtotime($data['_t1'])) {
			$data['_t1_status'] = 'red';
		}

		if ($data['status'] == 1 && strtotime($data['t1']) < time()) {
			$data['_t1_status'] = 'red';
			$data['_t2_status'] = 'red';
		}

		return $data;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_r);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}
			
			$row = $this->_view($id);
			if (!$this->_is_delete($row)) {
				continue;
			}

			$this->_delete3($id);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _delete3($id) {
		$db = newdb($this->dsn_w);
		if ($this->_is_admin()) {
			$fmt = "DELETE FROM %s WHERE id = '%s'";
		} else {
			$fmt = "UPDATE %s SET visible = 2 WHERE id = '%s'";
		}
		
		$sql = sprintf($fmt, $this->tbl, $id);
		debug($sql);
		$db->query($sql);
	}

	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}
		
		if ($row['status'] > 2) {
			return false;
		}

		if ($this->_audit('164')) {
			return true;
		}

		if ($row['emp'] == $_SESSION['id'] || $row['oper'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function update($controller, $request) {
		$this->pretreat($controller);

		if ($_SESSION['code'] <> 'admin') {
			$controller->assign("msg", "对不起，操作受限，请联系管理员。");
			$controller->display($request, 'info');
			return true;
		}

		$controller->display($request, 'mold/plan/update');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function updatePost($controller, $request) {
		$this->pretreat($controller);

		$from_admin = $request['from_admin'];
		$to_admin = $request['to_admin'];
		$from_emp = $request['from_emp'];
		$to_emp = $request['to_emp'];

		$db = newdb($this->dsn);
		if (!empty($from_admin) && !empty($to_admin)) {
			$fmt = "UPDATE %s SET admin = '%s' where admin = '%s' and status < 9";
			$sql = sprintf($fmt, Constant::tbl_mold_plan, $to_admin, $from_admin);
			$db->query($sql);
		}

		if (!empty($from_emp) && !empty($to_emp)) {
			$fmt = "UPDATE %s SET emp = '%s' where emp = '%s' and status < 9";
			$sql = sprintf($fmt, Constant::tbl_mold_plan, $to_emp, $from_emp);
			$db->query($sql);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function group($controller, $request) {
		$this->pretreat_group($controller);

		$controller->assign('t1', strtotime('-1 month'));

		$controller->display($request, 'mold/plan/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'mold');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'mold') {
			$row['a'] = sprintf("%s [%s]", Name::mold($row['a']), $row['a']);
		} elseif ($px == 'admin' || $px == 'emp') {
			$row['a'] = sprintf("%s [%s]", Name::emp($row['a']), $row['a']);
		} else {

			$row = $this->_name_t($row, $px);
		}

		return $row;
	}

	function _bar($controller, $request) {
		$this->pretreat($controller);

		$barcode_s = $request['barcode_s'];
		$aa = explode('.', $barcode_s);

		$id = $aa[2];

		$mold_b = $aa[1];
		$emp_b = $aa[3];
		$status_b = $aa[4];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);
		if (!$row) {
			$controller->assign("msg", "条码有误，请联系管理员");
			$controller->display($request, 'info');
			return false;
		}

		if ($row['status'] == '1' && $status_b == '3') {
			$controller->assign("msg", "生产还未开工，不能扫描完工条码，请重新操作");
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign("status", $status_b);
		/*
		 if ($row['emp'] <> $emp_b) {
			$controller->assign("msg", "条码有误，请联系管理员");
			$controller->display($request, 'info');
			return false;
			}*/

		if ($row['mold'] <> $mold_b) {
			$controller->assign("msg", "条码有误，请联系管理员");
			$controller->display($request, 'info');
			return false;
		}

		$row['piece'] = $this->piece[$row['piece']];
		$row['technics'] = $this->technics[$row['technics']];

		$row['emp_t'] = Name::emp($row['emp']);

		$time = $row['tt'];

		$row['hour'] = floor($time / 60);
		$row['minute'] = $time % 60;

		$row['status_t'] = $this->status[$row['status']];

		$controller->assign_by_ref('result', $row);

		require_once 'Mold.class.php';
		$mold = new Mold();
		$controller->assign_by_ref('mold', $mold->_view($row['mold']));

		if ($row['status'] == '9') {
			require_once 'Produce.class.php';
			$produce = new Produce();
			$controller->assign_by_ref('produce', $produce->_view($id));
		}

		$controller->display($request, 'mold/plan/barcode');
		return true;
	}

	function t($controller, $request) {
		$t1_s = date('Y-m-d', strtotime('-7 day'));
		echo $t1_s;

		logdate($controller, $request);
		return true;
	}

	function d($t1, $t2) {
		$a = date('z', $t1);
		$b = date('z', $t2);

		$y1 = date('Y', $t1);
		$y2 = date('Y', $t2);

		if ($y1 == $y2) {
			return $b - $a;
		}

		$t = 0;
		for ($i = $y1; $i < $y2; $i++) {
			$z = date('z', strtotime("$i-12-31"));
			// echo $z . "\n";
			$t += $z;
		}

		return $t - $a + 1 + $b;
	}

	function _report_status($id) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT status, COUNT(status) c FROM %s WHERE mold = '%s' GROUP BY status";
		$sql = sprintf($fmt, $this->tbl, $id);
		debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row['status_t'] = $this->status[$row['status']];

			$ret[] = $row;
		}

		return $ret;
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function ee($controller, $request) {
		$this->pretreat($controller);

		$controller->assign_by_ref('row', $this->_report());

		$controller->display($request, 'mold/plan/ee');
		// $controller->fetch($request, 'mold/plan/view');
		return true;
	}

	function _report($id) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE mold = '%s'";
		$sql = sprintf($fmt, $this->tbl, $id);
		// debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			if ($this->_report_1($row)) {
				continue;
			}

			$ret[] = $row;
		}

		return $ret;
	}

	function _report_1($data) {
		$dd = date('Y-m-d');

		$status = $data['status'];
		if ($status == '1') {
			if (strtotime($data['t1']) < strtottime($dd)) {
				return true;
			}

			return false;
		}

		if ($status == '2') {
			if (strtotime($data['t2']) < strtottime($dd)) {
				return true;
			}

			return false;
		}

		if ($status == '9') {
			return false;
		}

		return true;
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function report_daily($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		require_once 'Mold.class.php';
		$a = new Mold();
		$mold = $a->_view($id);
		$controller->assign_by_ref('mold', $mold);

		$from = $this->email;
		$email = 'list.a@tecmold.com';

		$fmt = '[%s]产品计划执行情况';
		$subject = sprintf($fmt, $mold['code']);
		$request['subject'] = $subject;

		$body = $this->_report_body($controller, $request);

		$this->mail_html($from, $email, $subject, $body);

		return true;
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function z($controller, $request) {
		$this->pretreat($controller);

		$request['id'] = 196;

		$request['tpl'] = "html4";

		$id = $request['id'];

		require_once 'Mold.class.php';
		$a = new Mold();
		$mold = $a->_view($id);
		$controller->assign_by_ref('mold', $mold);

		$from = $this->email;
		$email = 'sunxy@tecmold.com';

		$fmt = '[%s]产品计划执行情况';
		$subject = sprintf($fmt, $mold['code']);
		$request['subject'] = $subject;

		$body = $this->_report_body($controller, $request);

		debug($body);

		// $this->sendmail($from, $email, $subject, $body);

		return true;
	}

	function _report_body($controller, $request) {
		$id = $request['id'];

		$admin_s = $this->_input($request['admin_s']);
		$t1_s = date('Y-m-d', strtotime('-7 day'));

		$db = newdb($this->dsn_r);
		$sql = "SELECT * FROM " . $this->tbl;
		$where = " WHERE mold = '" . $id . "'";

		if (!empty($admin_s) && $admin_s <> '-') {
			$where .= " AND admin = '" . $admin_s . "'";
		}

		$where .= " AND t1 > '" . $t1_s . "'";

		$orderby = ' ORDER BY t1';
		$sql .= $where . $orderby;
		// debug($sql);
		$result = $db->query($sql);

		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row['admin_t'] = Name::emp($row['admin']);

			$row['mold_t'] = Name::mold($row['mold']);

			$row['piece'] = $this->piece[$row['piece']];
			$row['technics'] = $this->technics[$row['technics']];

			$row['emp_t'] = Name::emp($row['emp']);

			$time = $row['tt'];

			$row['hour'] = floor($time / 60);
			$row['minute'] = $time % 60;

			$row['status_t'] = $this->status[$row['status']];

			$row = $this->aa($row);

			$data[] = $row;
		}

		$controller->assign_by_ref('result', $data);
		return $controller->sunxy($request, 'mold/plan/report_daily');
	}

	/**
	 * 定时运行
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function tj_daily($controller, $request) {
		$this->pretreat($controller);

		$request['tpl'] = "html4";

		$from = $this->email;
		$to = 'sunxy@tecmold.com';

		$subject = '新增计划情况';

		$body = $this->_tj_a($controller, $request);

		$this->mail_html($from, $to, $subject, $body);

		logdate($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function _tj_a($controller, $request) {
		$this->pretreat($controller);

		$this->result_s = 20;
		$this->groupby_s = 'admin';
		$s = $this->groupby($controller, $this->groupby_s);
		$df = "DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d')";

		$fmt = "SELECT %s as a, COUNT(%s) as c FROM %s a WHERE createtime > %s GROUP BY %s ORDER BY c DESC";
		$sql = sprintf($fmt, $s, $s, $this->tbl, $df, $s);
		echo $sql . "\n";

		$this->_group_tj($controller, $sql);

		return $controller->sunxy($request, 'tj_a');
	}
	
	function ajax_worker($controller, $request) {
		$id = $_COOKIE['id_'];
		$ret = array();
		
		if ($this->_ajax_x($id, 'worker')) {
			$ret['status'] = 1;
		} else {
			$ret['status'] = 2;
		}

		echo json_encode($ret);
	}
	
	function tj_emp($controller, $request) {		
		$aa = $this->_list_a_emp();
		// print_r($aa);
		if (!$aa || count($aa) == 0) {
			echo "33";
			return false;
		}
		
		$from = $this->email;
		$subject = '任务信息(计划人)';
				
		$d2 = $this->_tj_df("createtime", "%Y-%m-%d");
		$d3 = $this->_tj_df("modifytime", "%Y-%m-%d");
		
		$dd = $this->_tj_df_not("%Y-%m-%d");
		
		while (list($k, $row) = each($aa)) {
			$emp = $row['emp'];
						
			$e = Common::emp($emp);
			$to = $e['email'];
			if (empty($to) || $e['status'] == '3') {
				continue;
			}
			
			// $to = "sunxy@tecmold.com";		
			$cc = array(
				'等待处理'=>array('w' => "status = '2' AND emp = '$emp'", 'b'=>'id DESC'),
				'正在处理'=>array('w' => "status = '3' AND emp = '$emp'", 'b'=>'id DESC'),
				'新增任务'=>array('w' => "$d2 AND emp = '$emp'", 'b'=>'id DESC'),
				'更新任务'=>array('w' => "$d3 AND $dd AND emp = '$emp'", 'b'=>'id DESC'),			
			);
			// print_r($cc);
			
			if ($this->_tj_cc($controller, $cc)) {
				echo ".";
				continue;
			}
	
			$body = $this->_tj_html($controller, $request, 'mold/plan/tj_list');
			
			echo $to . "\n";
			$this->mail_html($from, $to, $subject, $body);
		}

		logdate($controller, $request);
		return true;
	}
	
	function _list_a_emp() {		
		$db = newdb($this->dsn_r);
		// $fmt = "SELECT emp, COUNT(*) t FROM %s WHERE LENGTH(text) > 5 GROUP BY emp";
		$fmt = "SELECT emp, COUNT(*) t FROM %s WHERE status IN ('2', '3') GROUP BY emp";
		$sql = sprintf($fmt, $this->tbl);
		echo $sql . "\n";
		$result = $db->query($sql);
		
		return $db->fetch_all($result);
	}
	
	function tj_worker($controller, $request) {		
		$aa = $this->_list_a_worker();
		// print_r($aa);
		if (!$aa || count($aa) == 0) {
			echo "33";
			return false;
		}
		
		$from = $this->email;
		$subject = '任务信息(负责人)';
				
		$d2 = $this->_tj_df("createtime", "%Y-%m-%d");
		$d3 = $this->_tj_df("modifytime", "%Y-%m-%d");
		
		$dd = $this->_tj_df_not("%Y-%m-%d");
		
		while (list($k, $row) = each($aa)) {
			$emp = $row['worker'];
						
			$e = Common::emp($emp);
			$to = $e['email'];
			if (empty($to) || $e['status'] == '3') {
				continue;
			}
			
			// $to = "sunxy@tecmold.com";		
			$cc = array(
				'等待处理'=>array('w' => "status = '2' AND worker = '$emp'", 'b'=>'id DESC'),
				'正在处理'=>array('w' => "status = '3' AND worker = '$emp'", 'b'=>'id DESC'),
				'新增任务'=>array('w' => "$d2 AND worker = '$emp'", 'b'=>'id DESC'),
				'更新任务'=>array('w' => "$d3 AND $dd AND worker = '$emp'", 'b'=>'id DESC'),			
			);
			// print_r($cc);
			
			if ($this->_tj_cc($controller, $cc)) {
				echo ".";
				continue;
			}
	
			$body = $this->_tj_html($controller, $request, 'mold/plan/tj_list');
			
			echo $to . "\n";
			$this->mail_html($from, $to, $subject, $body);
		}

		logdate($controller, $request);
		return true;
	}
	
	function _list_a_worker() {		
		$db = newdb($this->dsn_r);
		// $fmt = "SELECT emp, COUNT(*) t FROM %s WHERE LENGTH(text) > 5 GROUP BY emp";
		$fmt = "SELECT worker, COUNT(*) t FROM %s WHERE status IN ('2', '3') GROUP BY worker";
		$sql = sprintf($fmt, $this->tbl);
		echo $sql . "\n";
		$result = $db->query($sql);
		
		return $db->fetch_all($result);
	}
	
	function view_mold($controller, $request) {
		$id = $request['id'];
	
		$ret = $this->_list_mold($id);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'mold/plan/_list_c');
		return true;
	}
	
	function view_emp($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
	
		$ret = $this->_list_emp($id);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'mold/plan/_list_c');
		return true;
	}
}
?>